package com.infore.airsupervise.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.vincent.filepicker.Util;

/**
 * 文件相关的工具类
 * Created by xiaotao on 2017/5/18.
 */

public class FileUtil {
    private static final String [][] fileType_MIME = {
            {"amr" , "audio/mp3"},
            {"doc", "application/msword"},
            {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
            {"xls", "application/vnd.ms-excel"},
            {"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
            {"jpeg", "image/jpeg"},
            {"jpg", "image/jpeg"},
            {"mp3", "audio/x-mpeg"},
            {"mp4", "video/mp4"},
            {"pdf", "application/pdf"},
            {"png", "image/png"},
    };

    public static String  getFileMIMEType(String fileSuffix){
        String fileMIME = "";
        if(!TextUtils.isEmpty(fileSuffix)){
            for(String[] types : fileType_MIME){
                if( TextUtils.equals(types[0],fileSuffix)){
                    fileMIME =  types[1];
                }
            }
        }
        return fileMIME;
    }

    public static void intentOpenFile(Context context ,String  filePath , String dataType){
        Uri uri = Uri.parse("file://" +filePath);
        Intent it = new Intent(Intent.ACTION_VIEW);
        it.setDataAndType(uri,dataType);
        if(Util.detectIntent(context,it)){
            context.startActivity(it);
        }else {
            ToastUtil.showShort(context,"对不起！没有找到可以打开这类文件的应用。");
        }
    }
}
